﻿using System;
using System.Windows.Forms;
using ConfigurationManagement.Data;
using ConfigurationManagement.MsBuild;
using Environment = ConfigurationManagement.Data.Environment;

namespace ConfigurationManagement.Administration.UI.Utilities
{
    public partial class ConfigureFileBasedOnDatabaseForm : Form
    {
        private readonly ConfigurationManagementDataContext _db = new ConfigurationManagementDataContext();
        private Environment _selectedEnvironment;
        private Project _selectedProject;
        private Solution _selectedSolution;

        /// <summary>
        /// 	Ctor
        /// </summary>
        public ConfigureFileBasedOnDatabaseForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 	Form load event handler
        /// </summary>
        /// <param name = "sender"></param>
        /// <param name = "e"></param>
        private void ConfigureFileBasedOnDatabaseFormLoad(object sender, EventArgs e)
        {
            LoadProjects();
        }

        /// <summary>
        /// 	Projects combo changed
        /// </summary>
        /// <param name = "sender"></param>
        /// <param name = "e"></param>
        private void ProjectsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ProjectsComboBox.SelectedIndex < 0) return;
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadSolutions();
            LoadEnvironments();
        }

        /// <summary>
        /// 	Solutions combo changed
        /// </summary>
        /// <param name = "sender"></param>
        /// <param name = "e"></param>
        private void SolutionsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            _selectedSolution = (Solution) SolutionsComboBox.SelectedItem;
        }

        /// <summary>
        /// 	Environments combo changed
        /// </summary>
        /// <param name = "sender"></param>
        /// <param name = "e"></param>
        private void EnvironmentsComboBoxSelectedIndexChanged(object sender, EventArgs e)
        {
            _selectedEnvironment = (Environment) EnvironmentsComboBox.SelectedItem;
        }

        /// <summary>
        /// 	Load projects
        /// </summary>
        private void LoadProjects()
        {
            ProjectsComboBox.DisplayMember = "ProjectName";
            ProjectsComboBox.ValueMember = "ProjectID";
            ProjectsComboBox.DataSource = _db.Projects;
            _selectedProject = (Project) ProjectsComboBox.SelectedItem;
            LoadSolutions();
            LoadEnvironments();
        }

        /// <summary>
        /// 	Load solutions
        /// </summary>
        private void LoadSolutions()
        {
            if (_selectedProject == null) return;
            SolutionsComboBox.DisplayMember = "SolutionName";
            SolutionsComboBox.ValueMember = "SolutionID";
            SolutionsComboBox.DataSource = _selectedProject.Solutions;
            _selectedSolution = (Solution) SolutionsComboBox.SelectedItem;
        }

        /// <summary>
        /// 	Load environments
        /// </summary>
        private void LoadEnvironments()
        {
            if (_selectedProject == null) return;
            EnvironmentsComboBox.DisplayMember = "EnvironmentName";
            EnvironmentsComboBox.ValueMember = "EnvironmentID";
            EnvironmentsComboBox.DataSource = _selectedProject.Environments;
            _selectedEnvironment = (Environment) EnvironmentsComboBox.SelectedItem;
        }

        /// <summary>
        /// 	Configure button
        /// </summary>
        /// <param name = "sender"></param>
        /// <param name = "e"></param>
        private void ConfigureButtonClick(object sender, EventArgs e)
        {
            if (_selectedProject == null
                || _selectedSolution == null
                || _selectedEnvironment == null)
            {
                MessageBox.Show(@"Please ensure you have selected a project, solution and environment");
                return;
            }

            var dr = openFileDialog1.ShowDialog();
            if (dr != DialogResult.OK)
                return;
            dr = saveFileDialog1.ShowDialog();
            if (dr != DialogResult.OK)
                return;

            var inputPath = openFileDialog1.FileName;
            var outputPath = saveFileDialog1.FileName;

            var msBuildTask = new ConfigureFileBasedOnDictionary
                                  {
                                      DictionaryConnectionString = _db.Connection.ConnectionString,
                                      Environment = _selectedEnvironment.EnvironmentName,
                                      HtmlEncode = HtmlEncodeCheckBox.Checked,
                                      InputFile = inputPath,
                                      OutputFile = outputPath,
                                      Project = _selectedProject.ProjectName,
                                      Solution = _selectedSolution.SolutionName,
                                      HostObject = new TaskHost(),
                                      BuildEngine = new BuildEngine()
                                  };
            msBuildTask.Execute();
            MessageBox.Show(@"Your file has been configured");
        }
    }
}